package com.feng.study.config;

import ch.sbb.esta.openshift.gracefullshutdown.GracefulShutdownHook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * 下线nacos
 *
 * @author zzm
 * @date 2021年3月25日
 */
public class GracefulNacosDeregister implements Runnable {

    private static final Logger logger = LoggerFactory.getLogger(GracefulNacosDeregister.class);

    private final ConfigurableApplicationContext applicationContext;

    public GracefulNacosDeregister(ConfigurableApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public void run() {
        logger.info("application shutdown hook start!");
        String waitSeconds = System.getProperty("estaGracefulShutdownWaitSeconds");
        System.out.println("waitSeconds:" + waitSeconds);
        nacosDeregister();
        GracefulShutdownHook gracefulShutdownHook = new GracefulShutdownHook(applicationContext);
        gracefulShutdownHook.run();
        logger.info("application shutdown hook end!");

    }

    private void nacosDeregister() {
        try {
            logger.info("执行nacos注销操作......");
//            logger.info("De-registering from Nacos Server now..." + DateUtil.format(new Date(), DateUtil.FULL_PATTERN));
//            NacosAutoServiceRegistration nacosDiscoveryProperties = applicationContext.getBean(NacosAutoServiceRegistration.class);
//            nacosDiscoveryProperties.stop();
//            logger.info("De-registering from Nacos Server end ..." + DateUtil.format(new Date(), DateUtil.FULL_PATTERN));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }

}
